package com.COMP3111H.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Home extends Activity {
	final int HELP = 0;
	boolean userLogin = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);

	}
	
	@Override
    protected void onResume() {
		super.onResume();
		if (LoginUtility.checkLoginStatus(this)) {
			RelativeLayout currentLayout = (RelativeLayout)findViewById(R.id.home);
			currentLayout.removeAllViews();
			
			LayoutInflater inflater = getLayoutInflater();
			currentLayout.addView(inflater.inflate(R.layout.home_logged_new, null));
			
			//setContentView(R.layout.home_logged_new);
			userLogin = true;

			Button buy_borrow = (Button) findViewById(R.id.buy_borrow);
			buy_borrow.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// Perform action on click
					Intent myIntent = new Intent(Home.this, Search.class);
					startActivity(myIntent);
				}
			});

			Button sell_lend = (Button) findViewById(R.id.sell_lend);
			sell_lend.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// Perform action on click
					Intent myIntent = new Intent(Home.this, Search.class);
					startActivity(myIntent);
				}
			});

			Button profile = (Button) findViewById(R.id.view_my_profile);
			profile.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// Perform action on click
					Intent myIntent = new Intent(Home.this, My_Profile.class);
					startActivity(myIntent);
				}
			});

			Button logout = (Button) findViewById(R.id.logout);
			logout.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// Perform action on click
					String[] usernamePassword = LoginUtility
							.getUsernamePassword(Home.this);
					Toast.makeText(Home.this,
							usernamePassword[0] + " logged out successful",
							Toast.LENGTH_SHORT).show();
					LoginUtility.logout(Home.this);
					Intent intent = getIntent();
					finish();
					startActivity(intent);
				}
			});
			
			Button menu = (Button) findViewById(R.id.home_menu);
			menu.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					 String[] choices={"View My Profile","Help","Cancel"};  
					             AlertDialog dialog = new AlertDialog.Builder(Home.this)  
					                     // .setIcon(android.R.drawable.btn_star)  
					                      .setTitle("Menu")  
					                      .setItems(choices, onselect_logged).create();  
					             dialog.show();  
				}
			});
		} else {
			RelativeLayout currentLayout = (RelativeLayout)findViewById(R.id.home);
			currentLayout.removeAllViews();
			
			LayoutInflater inflater = getLayoutInflater();
			currentLayout.addView(inflater.inflate(R.layout.home_not_logged_new, null));
			
			//setContentView(R.layout.home_not_logged_new);
			userLogin = false;

			Button buy_borrow = (Button) findViewById(R.id.Buy_Borrow);
			buy_borrow.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// Perform action on click
					Intent myIntent = new Intent(Home.this, Search.class);
					startActivity(myIntent);
				}
			});

			Button sell_lend = (Button) findViewById(R.id.Sell_Lend);
			sell_lend.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// Perform action on click
					Intent myIntent = new Intent(Home.this, Search.class);
					startActivity(myIntent);
				}
			});

			Button login = (Button) findViewById(R.id.Login);
			login.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent myIntent = new Intent(Home.this, Login.class);
					startActivityForResult(myIntent, 0);
				}
			});

			Button new_user = (Button) findViewById(R.id.New_User);
			new_user.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent myIntent = new Intent(Home.this,
							New_User_Registration.class);
					startActivity(myIntent);
				}
			});
			
			Button menu = (Button) findViewById(R.id.home_notlogged_menu);
			menu.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					 String[] choices={"Help","Cancel"};  
					             AlertDialog dialog = new AlertDialog.Builder(Home.this)  
					                      .setTitle("Menu")  
					                      .setItems(choices, onselect_not_logged).create();  
					             dialog.show();  
				}
			});
		}
	}
	
	OnClickListener onselect_logged = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			Intent myIntent;
			switch (which) {
			case 0:
				myIntent = new Intent(Home.this, My_Profile.class);
				startActivity(myIntent);
				break;
			case 1:
				myIntent = new Intent(Home.this, Help.class);
				startActivityForResult(myIntent, HELP);
				break;
			case 2:
				dialog.dismiss();
				break;
			}
		}
	};
	
	OnClickListener onselect_not_logged = new OnClickListener() {
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			Intent myIntent;
			switch (which) {
			case 0:
				myIntent = new Intent(Home.this, Help.class);
				startActivity(myIntent);
				break;
			case 1:
				dialog.dismiss();
				break;
			}
		}
	};
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case 0:
			Intent intent = getIntent();
			overridePendingTransition(0, 0);
			intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
			finish();

			overridePendingTransition(0, 0);
			startActivity(intent);
			break;

		}
	}
}
